#include <iostream>
#include <boost/program_options.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include <QApplication>
#include <QTimer>

#include "btrans_worker.h"
#include "WebBrowser.h"

namespace po = boost::program_options;
po::options_description desc("Allowed options");
po::variables_map vm;

void process_options(int argc, char** argv);
void usage(int exitcode = 0)
{
  std::cout << desc << std::endl;
  exit(exitcode);
}


int main(int argc, char** argv)
{
  std::cout << "hmm tests ..." << std::endl;

  process_options(argc, argv);
  QApplication a(argc, argv);
  WebBrowser web;
  web.page()->settings()->setAttribute(QWebSettings::AutoLoadImages, false);
  web.page()->settings()->setAttribute(QWebSettings::JavascriptEnabled, false);
  Worker w(vm, web);

  web.show();
  
  if (vm.count("help"))
    usage();
  else
  {
    if (vm.count("url"))
      QTimer::singleShot(0, &w, SLOT(do_url()));
    else if (vm.count("dumptext"))
      QTimer::singleShot(0, &w, SLOT(do_dumptext()));
  }

  return a.exec();
}

void process_options(int argc, char** argv)
{
  desc.add_options()
    ("help", "produce this usage")
    ("url", po::value<std::string>(), "input url")
    ("input", po::value<std::string>()->default_value("links.txt"), "input file")
    ("output", po::value<std::string>()->default_value("output.txt"), "output file")
    ("dumptext", "to dump all html content to a text file")
    ("prefix", po::value<std::string>(), "url prefix")
    ;
  po::store(po::parse_command_line(argc, argv, desc), vm);
  po::notify(vm);
}

